package com.kunbo.cn.controller.core.sale;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.kunbo.cn.entity.core.sale.OrderCarryOver;
import com.kunbo.cn.entity.core.vo.SalesOrders;
import com.kunbo.cn.service.core.sale.OrderCarryOverService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.retu.CommonResult;
import com.kunbo.cn.util.retu.ReturnBean;

/**
 *订单结转
 * @author bjl  
 * @date 2019年2月21日 
 */

@RestController
@RequestMapping("/OrderCarryOver")
public class OrdersCarryOverController {
	@Autowired
    private JdbcTemplate jdbcTemplate;
	@Autowired
	private OrderCarryOverService service;
	/**
	 * 添加结转单
	 * 
	 */
	@PostMapping("/add")
	public CommonResult add(OrderCarryOver carryOver) {
		service.save(carryOver);
		return new CommonResult(true,"操作成功",null);
	}
	
	/**
	 * 生成销售订单结转编码
	 * 
	 */
	@GetMapping("/createCode")
	public String createCode(){
		 String prex = "XSJZ";
		 String noticeCode= prex+SDate.getSystemDateYMDHMSToString();
		return noticeCode;
	}
	
	@PutMapping("/update")
	public CommonResult update(OrderCarryOver carryOver) {
		service.update(carryOver);
		return new CommonResult(true,"操作成功",null);
	}
	
	/**
	 *修改审核状态 
	 * 
	 */
	@PutMapping("/updateState")
	public CommonResult update1(OrderCarryOver carryOver) {
		service.updateExamineState(carryOver);
		return new CommonResult(true,"操作成功",null);
	}
	
	@DeleteMapping("/delete")
	public CommonResult delete(String id) {
		service.delete(id);
		return new CommonResult(true,"操作成功",null);
	}
	
	@GetMapping("/findById")
	public OrderCarryOver findById(String id) {
		OrderCarryOver d = service.findById(id);
		return d;
	}
	
	/**
	 *条件分页查询
	 * 
	 */
	@GetMapping("/findCarryOver")
	public Page<OrderCarryOver> findCarryOver(String noticeCode,Integer pageNo,Integer pageSize) {
		Page<OrderCarryOver> page = service.findOrderCarryOver(noticeCode, pageNo, pageSize);
		return page;
	}
	
	/**
	 * 销售订单下拉选
	 * 
	 */
	@GetMapping("/getSalesOrder") 
	public  List<SalesOrders> getSalesOrder(HttpServletRequest request,HttpServletResponse response) throws SQLException{  
		return service.getSalesOrder(request,response);
	} 
		/**
		 *
		 * 1.修改销售订单余量结转后订单余量(包括调量订单,调入订单余量)
		 * 2.先判断调量，调入订单号是否为空;
		 * 3.查询两个订单号两种材料是否一致
		 * 4.查询调量订单余量，查询调入订单余量，判断调量订单余量是否大于调整量
		 * 5.执行调量
		 * 
		 */
	   @PostMapping("/updateOrderNum")
	 public int updateOrderNum(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		   String OldOrder = request.getParameter("oldOrder"); //调量订单
		   String newOrder = request.getParameter("newOrder");//调入订单
		   return service.updateOrderNum(OldOrder,newOrder);
		
	   }

		/**
		 *
		 * (添加运单即运单拆分)修改销售订单余量
		 * 
		 */
	@PostMapping("/updateOrderMargin")
	 public int updateOrderMargin(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		  return service.updateOrderMargin(request,response);
     }
	/**
	 *
	 * (编辑运单即运单拆分)修改销售订单余量
	 * 
	 */
		@PostMapping("/editOrderMargin")
		public int editOrderMargin(HttpServletRequest request,HttpServletResponse response) throws SQLException{
			 return service.editOrderMargin(request,response);
		}
		
		@RequestMapping("/startDnm")
		public ReturnBean startOrder(Integer id) {
			if( null==id) {
				return new ReturnBean(false,"参数有误，请检测");
			}
			Integer r = jdbcTemplate.update("business_delivery_notice_manage set state = '4'  where id = "+id+"");
			if(r>0) {
				return new ReturnBean(true,"操作成功");
			}else {
				return new ReturnBean(false,"执行失败");
			}
			
			
		}
		
		@RequestMapping("/cancelOrder")
		public ReturnBean cancelOrder(Integer id) {
			if( null==id) {
				return new ReturnBean(false,"参数有误，请检测");
			}
			Integer r = jdbcTemplate.update("update business_delivery_notice_manage set state = '5'  where id = "+id+"");
			if(r>0) {
				return new ReturnBean(true,"操作成功");
			}else {
				return new ReturnBean(false,"执行失败");
			}
			
			
		}
		
		@RequestMapping("/finishOrder")
		public ReturnBean finishOrder(Integer id) {
			if( null==id) {
				return new ReturnBean(false,"参数有误，请检测");
			}
			Integer r = jdbcTemplate.update("update business_delivery_notice_manage set state = '3'  where id = "+id+"");
			if(r>0) {
				return new ReturnBean(true,"操作成功");
			}else {
				return new ReturnBean(false,"执行失败");
			}
			
			
		}
}
